package com.myapp.ruyixing.ui;

import java.util.Calendar;

import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TextView;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.ui.widget.DatePickerFragment;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.TimeUtils;

public class AddPassengerActivity extends BaseActivity implements
		OnDateSetListener {

	private TextView mNameView;
	private Spinner mTypeView;
	private TextView mNumberView;
	private TextView mBirthday;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_air_ticket_add_passenger);

		mNameView = (TextView) findViewById(R.id.edit_chinese_name);
		mNumberView = (TextView) findViewById(R.id.edit_card_number);

		mTypeView = (Spinner) findViewById(R.id.edit_card_type);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.passenger_add_type,
				android.R.layout.simple_spinner_dropdown_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mTypeView.setAdapter(adapter);
		mTypeView.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				String item = (String) mTypeView.getAdapter().getItem(position);
				if ("身份证".equals(item)) {
					findViewById(R.id.birthday).setVisibility(View.GONE);
				} else {
					findViewById(R.id.birthday).setVisibility(View.VISIBLE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});

		mBirthday = (TextView) findViewById(R.id.edit_birthday);
		findViewById(R.id.birthday).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pickDate();
			}
		});

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});

		((TitleBar) findViewById(R.id.title_bar))
				.setOnRightIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						String name = mNameView.getText().toString();
						String number = mNumberView.getText().toString();
						String type = (String) mTypeView.getSelectedItem();
						String birthday = mBirthday.getText().toString();

						if (TextUtils.isEmpty(name)
								|| TextUtils.isEmpty(number)
								|| TextUtils.isEmpty(type)) {
							return;
						}

						Intent data = new Intent();
						Bundle bundle = new Bundle();
						bundle.putString("name", name);
						bundle.putString("code", number);
						bundle.putString("type", type);
						bundle.putString("birthday", birthday);
						data.putExtras(bundle);

						setResult(RESULT_OK, data);
						
						finish();
					}
				});
	}

	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		Calendar mPickDateCalendar = TimeUtils.getCalendar(year, monthOfYear,
				dayOfMonth);
		mBirthday.setText(TimeUtils.getString(mPickDateCalendar, "yyyy-MM-dd")
				.toString());
	}

	private void pickDate() {
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		Fragment prev = getSupportFragmentManager().findFragmentByTag(
				"datepicker");
		if (prev != null) {
			return;
		}
		ft.addToBackStack(null);

		// Create and show the dialog.
		DialogFragment newFragment = DatePickerFragment.getDatePickerFragment();

		newFragment.show(ft, "datepicker");
	}
}
